*****************************************************************************************************
************                     Replication Code (Version 1.0) For:                   **************
************ Political Alignment and the Allocation of Stock Market Resources in China **************
*****************************************************************************************************

*** This .do file reproduces: (a) Table 7 in the main text;
***                           (b) Table C3, C4(Panel A), E1 to E4, and G1 to G7 in the online appendix;
***                           (c) Figure E1 in the online appendix.

clear
set matsize 11000
set more off

use City_Panel.dta 

*******************************
***** Data Pre-processing *****
*******************************

** Recode city-level control variables
replace gdp=gdppc*pop if gdp==.
gen lggdp=ln(gdp + 1)
gen lggdppc=ln(gdppc + 1)
gen lgpop=ln(pop + 1)
gen linv = ln(investment + 1)
gen lgovrev = ln(govrev + 1)
gen lgovexp = ln(govexp + 1)
gen unerate = unemployment / (pop * 100)
replace corrupt = 1 if corrupt2 == "贪污腐败"
egen city_code = group(cityID)
egen pro_code = group(province)
bysort city_code: gen ipofail_adj = ipowait_adj - ipo_adj
replace ipofail_adj = 0 if ipofail_adj < 0

** City official characteristics
replace age=. if age>2000
bysort ps: sum(prom0 connection_prom age)
sort ps cityID year
gen tenure=0
replace tenure=tenure[_n-1]+1 if name[_n]==name[_n-1]  
gen first = (tenure == 0)
egen sex = group(c_sex)
egen edu = group(c_highest_edu)
egen mayor = group(name)
egen psec = group(current_psec)

keep if ps==0  // keep only data for city mayors (drop CPS's)

***** Appendix Table E1 *****
sum ipo_adj ipowait_adj ipoapply
sum connection_prom prom0 age sex edu tenure first 
sum lgpop lggdp lggdppc gdpgrowth linv lgovrev lgovexp unerate
sum corrupt level_ju princeling discount


*************************
***** Data Analysis *****
*************************
sort city_code, stable
xtset city_code year 

global city_vars L.lgpop L.lggdp L.lggdppc L.gdpgrowth L.lgovrev L.lgovexp L.linv L.unerate ipowait_adj
global mayor_vars first c.age##c.age c.tenure##c.tenure i.sex i.edu

***** Appendix Table C3 *****
eststo clear
foreach x in $city_vars {
    eststo: qui xtreg connection_prom `x' i.year if year>2003, fe cluster(city_code)
}
esttab, se b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) drop(*.year ipowait_adj) replace

***** Appendix Table C4 (Panel A)
** New IPO applications per year
eststo clear
eststo: qui xtreg ipoapply connection_prom $city_vars i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipoapply connection_prom $city_vars $mayor_vars i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipoapply connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(connection_prom) replace

***** Appendix Table E2 *****
eststo clear
eststo: qui xtreg ipo_adj connection_prom i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipo_adj connection_prom $city_vars i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipo_adj connection_prom $city_vars $mayor_vars i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) scalars(N_clust) star(* 0.1 ** 0.05 *** 0.01) keep(connection_prom) replace
estadd ysumm

***** Figure E1 *****
capture drop post? prior? conn?
* with connection in years after (no connection now)
g prior1=(connection_prom==0 & F.connection_prom==1)
g prior2=(connection_prom==0 & F.connection_prom==0 & F2.connection_prom==1)
g prior3=(connection_prom==0 & F.connection_prom==0 & F2.connection_prom==0 & F3.connection_prom==1)
g prior4=(connection_prom==0 & F.connection_prom==0 & F2.connection_prom==0 & F3.connection_prom==0 & F4.connection_prom==1)
g prior5=(connection_prom==0 & prior1==0 & prior2==0 & prior3==0 & prior4 == 0)
* currently connected
g conn1=(connection_prom==1 & L.connection_prom==0)
g conn2=(connection_prom==1 & L.connection_prom==1 & L2.connection_prom==0)
g conn3=(connection_prom==1 & L.connection_prom==1 & L2.connection_prom==1 & L3.connection_prom==0)
g conn4=(connection_prom==1 & conn1==0 & conn2==0 & conn3==0)

* prior1 as reference group
xtreg ipo_adj prior5 prior4 prior3 prior2 conn1-conn4 $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
* 95% CI
coefplot, keep(prior5 prior4 prior3 prior2 conn1 conn2 conn3 conn4) ///
	coeflabels(prior5="<=-5" prior4="-4" prior3="-3" ///
	prior2="-2" conn1="0" conn2="1" ///
	conn3="2" conn4=">=3") ///
	ytitle("Effect of Political Alignment on IPO Approval") ///
	xtitle("Year relative to the arrival of a politically aligned mayor") ///
	levels(95) ///
	vertical ///
	yline(0, lp(dash)) ///
	xline(4.5, lp(dash)) ///
	addplot(line @b @at) ///
	ciopts(recast(rcap)) ///
	graphregion(color(gs16)) ///
	ylabel(#5, nogrid) ///
	scheme(s2mono)

***** Appendix Table E4 *****
eststo clear
eststo: qui xtreg ipo_adj connection_prom i.psec i.year if year>2003, fe cluster(city_code)   // PPS FE
eststo: qui xtreg ipo_adj connection_prom $city_vars $mayor_vars i.psec i.year if year>2003, cluster(city_code)   // PPS FE
eststo: qui xtreg ipo_adj connection_prom $city_vars $mayor_vars i.psec i.year i.pro_code##c.year if year>2003, cluster(city_code)   // PPS FE
eststo: qui xtreg ipo_adj connection_prom i.mayor i.year if year>2003, fe cluster(city_code)   // mayor FE
eststo: qui xtreg ipo_adj connection_prom $city_vars i.mayor i.year if year>2003, cluster(city_code)    // mayor FE
eststo: qui xtreg ipo_adj connection_prom $city_vars i.mayor i.year i.pro_code##c.year if year>2003, cluster(city_code)    // mayor FE
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(connection_prom) replace

***** Table 7 *****
** Code mayor's promotion within 0-3 years
sort cityID year, stable
gen sameid1=0
replace sameid1=1 if name[_n]==name[_n+1]
gen prom1=0
replace prom1=1 if sameid1==1 & prom0[_n+1]==1
replace prom1=1 if prom0==1   // promotion in one year after

gen sameid2=0
replace sameid2=1 if name[_n]==name[_n+1] & name[_n]==name[_n+2]
gen prom2=0
replace prom2=1 if sameid1==1 & prom0[_n+1]==1
replace prom2=1 if sameid2==1 & prom0[_n+2]==1
replace prom2=1 if prom0==1   // promotion in two years after

gen sameid3=0
replace sameid3=1 if name[_n]==name[_n+1] & name[_n]==name[_n+2] & name[_n]==name[_n+3]
gen prom3=0
replace prom3=1 if sameid1==1 & prom0[_n+1]==1
replace prom3=1 if sameid2==1 & prom0[_n+2]==1
replace prom3=1 if sameid3==1 & prom0[_n+3]==1
replace prom3=1 if prom0==1   // promotion in three years after

replace prom1=. if prom0==.
replace prom2=. if prom0==.
replace prom3=. if prom0==.

xtset city_code year
eststo clear
eststo: qui xtreg prom0 ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom1 ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom2 ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom3 ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj) replace
estadd ysumm

***** Appendix Table G1 *****
gen w1 = (pro_code == 11 | pro_code == 22 | pro_code == 15 | pro_code == 24 | ///
          pro_code == 6 | pro_code == 8 | pro_code == 12 | pro_code == 19 | pro_code == 14 | pro_code == 1 | pro_code == 2 | ///
		  pro_code == 10 | pro_code == 13 | pro_code == 21)		  
gen w2 = (w1==0)
capture drop pro_party_congress
gen pro_party_congress = 0								   
replace pro_party_congress = 1 if (year == 2005 | year == 2010 | year == 2015) & w1 == 1
replace pro_party_congress = 1 if (year == 2006 | year == 2011 | year == 2016) & w2 == 1

eststo clear
eststo: qui xtreg prom0 ipo_adj connection_prom $city_vars $mayor_vars pro_party_congress c_central_exp i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom1 ipo_adj connection_prom $city_vars $mayor_vars pro_party_congress c_central_exp i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom2 ipo_adj connection_prom $city_vars $mayor_vars pro_party_congress c_central_exp i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom3 ipo_adj connection_prom $city_vars $mayor_vars pro_party_congress c_central_exp i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj pro_party_congress c_central_exp) replace
estadd ysumm

***** Appendix Table G3 *****
eststo clear
eststo: qui xtreg prom0 ipofail_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom1 ipofail_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom2 ipofail_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg prom3 ipofail_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipofail_adj) replace
estadd ysumm

***** Appendix Table G4 ***** 
** Create lagged dependent variable: Mayor's promotion in previous 1-3 years
capture drop preprom? samecityID?
sort cityID year, stable

gen samecityID1=0
replace samecityID1=1 if cityID[_n]==cityID[_n-1]
gen preprom1=0
replace preprom1=1 if samecityID1==1 & prom0[_n-1]==1  // promotion in 1 year before

gen samecityID2=0
replace samecityID2=1 if cityID[_n]==cityID[_n-1] & cityID[_n]==cityID[_n-2]
gen preprom2=0
replace preprom2=1 if samecityID1==1 & prom0[_n-1]==1
replace preprom2=1 if samecityID2==1 & prom0[_n-2]==1  // promotion in 2 years before

gen samecityID3=0
replace samecityID3=1 if cityID[_n]==cityID[_n-1] & cityID[_n]==cityID[_n-2] & cityID[_n]==cityID[_n-3]
gen preprom3=0
replace preprom3=1 if samecityID1==1 & prom0[_n-1]==1
replace preprom3=1 if samecityID2==1 & prom0[_n-2]==1
replace preprom3=1 if samecityID3==1 & prom0[_n-3]==1  // promotion in 3 years before

replace preprom1=. if prom0==.
replace preprom2=. if prom0==.
replace preprom3=. if prom0==.

xtset city_code year
global city_vars lgpop lggdp lggdppc gdpgrowth lgovrev lgovexp linv unerate ipowait_adj
global mayor_vars first c.age##c.age c.tenure##c.tenure i.sex i.edu

eststo clear
eststo: qui xtreg preprom1 ipo_adj L.connection_prom L.($mayor_vars) L2.($city_vars) i.pro_code##c.year i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg preprom2 ipo_adj L2.connection_prom L2.($mayor_vars) L3.($city_vars) i.pro_code##c.year i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg preprom3 ipo_adj L3.connection_prom L3.($mayor_vars) L4.($city_vars) i.pro_code##c.year i.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj) replace
estadd ysumm

***** Appendix Table G5 *****
gen above57 = 0 if age !=.
replace above57 = 1 if age >= 57 & age !=.
gen pc57 = connection_prom * above57

global city_vars L.lgpop L.lggdp L.lggdppc L.gdpgrowth L.lgovrev L.lgovexp L.linv L.unerate ipowait_adj
global mayor_vars first c.age##c.age c.tenure##c.tenure i.sex i.edu

eststo clear
eststo: qui xtreg ipo_adj connection_prom pc57 above57 i.year if year>2003, fe cluster(cityID)
   test connection_prom + pc57 = 0
eststo: qui xtreg ipo_adj connection_prom pc57 above57 $city_vars i.year if year>2003, fe cluster(city_code)
   test connection_prom + pc57 = 0
eststo: qui xtreg ipo_adj connection_prom pc57 above57 $city_vars $mayor_vars i.year if year>2003, fe cluster(city_code)
   test connection_prom + pc57 = 0
eststo: qui xtreg ipo_adj connection_prom pc57 above57 $city_vars $mayor_vars i.pro_code##c.year i.year if year>2003, fe cluster(city_code)
   test connection_prom + pc57 = 0
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(connection_prom pc57) replace

***** Appendix Table G6 *****
gen above58 = 0 if age !=.
replace above58 = 1 if age >= 58 & age !=.
gen pc58 = connection_prom * above58
gen above56 = 0 if age !=.
replace above56 = 1 if age >= 56 & age !=.
gen pc56 = connection_prom * above56

eststo clear
eststo: qui xtreg ipo_adj connection_prom pc58 above58 $city_vars $mayor_vars i.pro_code##c.year i.year if year>2003, fe cluster(city_code)
   test connection_prom + pc58 = 0
eststo: qui xtreg ipo_adj connection_prom pc56 above56 $city_vars $mayor_vars i.pro_code##c.year i.year if year>2003, fe cluster(city_code)
   test connection_prom + pc56 = 0
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(connection_prom pc58 pc56) replace

***** Appendix Table G7 *****
*** Panel A
eststo clear
eststo: qui xtreg corrupt ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F.corrupt ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F2.corrupt ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj) replace
*** Panel B
eststo clear
eststo: qui xtreg level_ju ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F.level_ju ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F2.level_ju ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj) replace
*** Panel C
eststo clear
eststo: qui xtreg princeling ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F.princeling ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F2.princeling ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj) replace
*** Panel D
eststo clear
eststo: qui xtreg discount ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F.discount ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
eststo: qui xtreg F2.discount ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(ipo_adj) replace

save City_Panel2.dta

***** Appendix Table E3 *****
by city_code: egen ipo_max = mean(ipowait_adj)
drop if ipo_max == 0

eststo clear
eststo: qui xtreg ipo_adj connection_prom i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipo_adj connection_prom $city_vars i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipo_adj connection_prom $city_vars $mayor_vars i.year if year>2003, fe cluster(city_code)
eststo: qui xtreg ipo_adj connection_prom $city_vars $mayor_vars i.year i.pro_code##c.year if year>2003, fe cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(connection_prom) replace
estadd ysumm

***** Appendix Table G2 *****
** Aggregate By Mayor-Term (cross-section)
clear
use City_Panel2.dta

gen city_mayor = cityID + "_" + name
bysort city_mayor: egen mean_ipo = mean(ipo_adj)
bysort city_mayor: egen tenure_yrs = max(tenure)
replace tenure_yrs = tenure_yrs + 1
gen total_ipo = mean_ipo * tenure_yrs

sort city_mayor tenure, stable
gen first_last = 1 if (city_mayor != city_mayor[_n-1] )
replace first_last = 2 if (city_mayor != city_mayor[_n+1] )
keep if first_last == 1 | first_last == 2 

capture drop pre_*
sort city_mayor first_last, stable
foreach x in lgpop lggdp lggdppc gdpgrowth lgovrev lgovexp linv unerate year age sex edu connection_prom {
   gen pre_`x' = `x'[_n-1] if city_mayor == city_mayor[_n-1] & first_last == 2
   replace pre_`x' = `x' if city_mayor != city_mayor[_n-1] & first_last == 2
}
global city_vars2 pre_lgpop pre_lggdp pre_lggdppc pre_gdpgrowth pre_lgovrev pre_lgovexp pre_linv pre_unerate
global mayor_vars2 i.pre_year pre_age i.pre_sex i.pre_edu tenure_yrs pre_connection_prom

eststo clear
eststo: qui areg prom0 mean_ipo if first_last == 2, a(city_code) cluster(city_code)
eststo: qui areg prom0 mean_ipo i.year if first_last == 2, a(city_code) cluster(city_code)
eststo: qui areg prom0 mean_ipo $mayor_vars2 i.year if first_last == 2, a(city_code) cluster(city_code)
eststo: qui areg prom0 mean_ipo $city_vars2 $mayor_vars2 i.year if first_last == 2, a(city_code) cluster(city_code)
esttab, se b(3) t(3) star(* 0.1 ** 0.05 *** 0.01) keep(mean_ipo) replace
estadd ysumm
